package cn.stylefeng.guns.sys.modular.system.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.listener.DywhExcelListener;
import cn.stylefeng.guns.sys.core.util.JsonResult;
import cn.stylefeng.guns.sys.modular.system.entity.BtyDywh;
import cn.stylefeng.guns.sys.modular.system.entity.Yybb;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyDywhMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyDywhParam;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyDywhResult;
import cn.stylefeng.guns.sys.modular.system.service.BtyDywhService;
import cn.stylefeng.guns.sys.modular.system.service.YybbService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;


/**
 * 控制器
 * @author liuy
 * @Date 2021-12-31 13:52:22
 */
@Controller
@RequestMapping("/btyDywh")
public class BtyDywhController extends BaseController {

    private String PREFIX = "/modular/system/btyDywh";

    @Autowired
    private BtyDywhService btyDywhService;
    @Resource
    private BtyDywhMapper dywhMapper;
    @Resource
    private YybbService yybbService;

    /**
     * 跳转到主页面
     *
     * @author liuy
     * @Date 2021-12-31
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/btyDywh.html";
    }

    /**
     * 新增页面
     *
     * @author liuy
     * @Date 2021-12-31
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/btyDywh_add.html";
    }

    /**
     * 编辑页面
     *
     * @author liuy
     * @Date 2021-12-31
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/btyDywh_edit.html";
    }

    /**
     * 新增接口
     *
     * @author liuy
     * @Date 2021-12-31
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(BtyDywhParam btyDywhParam) {
        this.btyDywhService.add(btyDywhParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author liuy
     * @Date 2021-12-31
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(BtyDywhParam btyDywhParam) {
        this.btyDywhService.update(btyDywhParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author liuy
     * @Date 2021-12-31
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(BtyDywhParam btyDywhParam) {
        this.btyDywhService.delete(btyDywhParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author liuy
     * @Date 2021-12-31
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(BtyDywhParam btyDywhParam) {
        BtyDywh detail = this.btyDywhService.getById(btyDywhParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author liuy
     * @Date 2021-12-31
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(BtyDywhParam btyDywhParam) {
        return this.btyDywhService.findPageBySpec(btyDywhParam);
    }

    /**
     * 导入excel
     */
    @RequestMapping("/import")
    @ResponseBody
    public Object addUsers(@RequestParam("file") MultipartFile file) {
        JsonResult jsonResult = new JsonResult();
        //导入之前删掉之前的数据
        dywhMapper.delete(new QueryWrapper<>());
        try {
            DywhExcelListener basicExcelListener = new DywhExcelListener(btyDywhService);
            EasyExcel.read(file.getInputStream(), BtyDywh.class, basicExcelListener).sheet(0).doRead();
            //导入成功之后更新抖音区域收款金额到yybb中
            List<BtyDywhResult> listQy=yybbService.dyWhList(1);
            if(listQy.size()>0){
                for (BtyDywhResult wh : listQy) {
                    Integer id=wh.getId();
                    BigDecimal yye=wh.getYye();
                    BigDecimal sxf=wh.getSxf();
                    BigDecimal dzje=wh.getDzje();
                    String dm=wh.getDm();
                    Yybb yybb=yybbService.getById(id);
                    if(ToolUtil.isNotEmpty(yybb)){
                        yybb.setDyqyyye(yye);
                        yybb.setDyqysxf(sxf);
                        yybb.setDyqysj(dzje);
                        Boolean flag= yybbService.updateById(yybb);
                        if(flag==true){
                            List<BtyDywh>list=btyDywhService.list(new QueryWrapper<BtyDywh>().eq("dm",dm).eq("state",1));
                            if(list.size()>0){
                                BtyDywhParam param=new BtyDywhParam();
                                param.setDm(list.get(0).getDm());
                                param.setState(1);
                                btyDywhService.updateByDm(param);
                            }
                        }
                    }
                }
            }

            //导入成功之后更新抖音全国收款金额到yybb中
            List<BtyDywhResult> listQg=yybbService.dyWhList(2);
            if(listQg.size()>0){
                for (BtyDywhResult wh : listQg) {
                    Integer id=wh.getId();
                    BigDecimal yye=wh.getYye();
                    BigDecimal sxf=wh.getSxf();
                    BigDecimal dzje=wh.getDzje();
                    String dm=wh.getDm();
                    Yybb yybb=yybbService.getById(id);
                    if(ToolUtil.isNotEmpty(yybb)){
                        yybb.setDyyye(yye);
                        yybb.setDysxf(sxf);
                        yybb.setDy(dzje);
                        Boolean flag= yybbService.updateById(yybb);
                        if(flag==true){
                            List<BtyDywh>list=btyDywhService.list(new QueryWrapper<BtyDywh>().eq("dm",dm).eq("state",2));
                            if(list.size()>0){
                                BtyDywhParam param=new BtyDywhParam();
                                param.setDm(list.get(0).getDm());
                                param.setState(2);
                                btyDywhService.updateByDm(param);
                            }
                        }
                    }
                }
            }

            //导入成功之后更新抖音团购收款金额到yybb中
            List<BtyDywhResult> listTg=yybbService.dyWhList(3);
            if(listTg.size()>0){
                for (BtyDywhResult wh : listTg) {
                    Integer id=wh.getId();
                    BigDecimal yye=wh.getYye();
                    BigDecimal sxf=wh.getSxf();
                    BigDecimal dzje=wh.getDzje();
                    String dm=wh.getDm();
                    Yybb yybb=yybbService.getById(id);
                    if(ToolUtil.isNotEmpty(yybb)){
                        yybb.setDytgyye(yye);
                        yybb.setDytgsxf(sxf);
                        yybb.setDytgsj(dzje);
                        Boolean flag= yybbService.updateById(yybb);
                        if(flag==true){
                            List<BtyDywh>list=btyDywhService.list(new QueryWrapper<BtyDywh>().eq("dm",dm).eq("state",3));
                            if(list.size()>0){
                                BtyDywhParam param=new BtyDywhParam();
                                param.setDm(list.get(0).getDm());
                                param.setState(3);
                                btyDywhService.updateByDm(param);
                            }
                        }
                    }
                }
            }

//            //导入成功之后更新抖音区域收款金额到yybb中
//            dywhMapper.updateYybb(1);
//            //导入成功之后更新抖音全国收款金额到yybb中
//            dywhMapper.updateYybb(2);
//            //导入成功之后更新抖音团购收款金额到yybb中
//            dywhMapper.updateYybb(3);
            //System.out.println("num:"+num);
        }catch (Exception e) {
            e.printStackTrace();
            jsonResult.setState(0);
            jsonResult.setErrmsg(e.getMessage());
            return jsonResult;
        }
        return true;
    }

    @RequestMapping("/listSearchExport")
    @ResponseBody
    public List<BtyDywhResult> listSearchExport (BtyDywhParam param){
        List<BtyDywhResult> list= btyDywhService.customList(param);
        return list;
    }

}


